<?php

require_once dbget_path( 'mod', 'confizuz' );
require_once dbput_path( 'mod', 'confizuz' );
require_once util_path( 'mod', 'confizuz' );

function mod_install( $mod, $suggests='false' ) {
  $minfo = mod_readinfo( mod_path($mod,'info',$GLOBALS["${pre}NAV"]['MOD_FILE_TYPE']) );

  $deps = mod_getDepends($minfo);
  foreach( $deps as $depmod => $depver ) 
    mod_install( $depmod );

  require_once mod_path('mod','install');
  if( install() )
    setModStatus($minfo['name'], $minfo['ver'], 1);
  
  if( $suggests ) {
    $sugs = mod_getSuggests( $minfo);
    foreach( $sugs as $sugmod => $sugver )
      mod_install( $sugmod );
  }

}

function mod_getSuggests( $minfo, &$mods = array() ) {
  if( count( $minfo['suggests'] ) > 0 ) {
    foreach($minfo['suggests'] as $depmod => $depver) {
      list($verlow, $verhi) = explode('-', $depver);
      $dminfo = mod_readinfo( mod_path($depmod,'info',$GLOBALS["${pre}NAV"]['MOD_FILE_TYPE']) );
      if( mod_versionBetween($verlow , $dminfo['version'], $verhi) ) {
        $mod[$depmod] = $dminfo['version']; 
        mod_getDepends( $dminfo, $mods );
      } else
        return false;
    }
  }
  return $mods;
} 
function mod_getDepends( $minfo, &$mods = array() ) {
  if( count( $minfo['depends'] ) > 0 ) {
    foreach($minfo['depends'] as $depmod => $depver) {
      list($verlow, $verhi) = explode('-', $depver);
      $dminfo = mod_readinfo( mod_path($depmod,'info',$GLOBALS["${pre}NAV"]['MOD_FILE_TYPE']) );
      if( mod_versionBetween($verlow , $dminfo['version'], $verhi) ) {
        $mod[$depmod] = $dminfo['version']; 
        mod_getDepends( $dminfo, $mods );
      } else
        return false;
    }
  }
  return $mods;
} 
function mod_versionBetween( $vlow, $ver, $vhi ) {
  return ($vlow < $ver < $vhi);
}

/*
// fucking nope. None of this is good.
function mod_install( $mod, $bounds=array(), $installed=null ) {

  if(!is_array($installed))
    $installed = get_install_list();
  if( isset($installed[$mod]) ) )
    break;

  $meta = confizuz_load_meta( $mod );

  foreach( confizuz_depends($meta) as $dep => $depbounds) {
    $depbounds['suggest'] = $bounds['suggest'];
    //mod_install( $dep, $depbounds, &$installed );
    if( !($installed = mod_install( $dep, $depbounds, $installed )) )
      return false;
  }

  if( mod_run_install( $mod ) )
    $installed[$mod] = confizuz_mod_version($meta);
    put_new_mod_list( $installed );
  else
    return false;

  if( $bounds['suggests'] )
    foreach( confizuz_depends($meta) as $sug => $sugbounds) { 
      $depbounds['suggest'] = $bounds['suggest'];
      $installed_tmp = mod_install( $sug, $sugbounds, $installed )
    }

  $installed = array_diff_key($installed,get_install_list());

  return $installed;
}

function mod_run_install( $mod ) ) {

  return true;
}
*/
?>
